package com.java.common.utils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author zyz
 */
public class RandomUtil {

    public static final String KEY = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    public static final String CAPITAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 随机生成订单编号时间戳加一个大于等于100的随机数
     *
     * @return String
     * @Title: getUnique
     */
    public static String getUnique() {
        int maxNum = 100;
        Random r1 = new Random();
        int random1 = 0;
        while (random1 < maxNum) {
            random1 = r1.nextInt(1000);
        }
        String unique = "D" + System.currentTimeMillis() + (random1);
        return unique;
    }

    public static String getUnique(String t) {
        int maxNum = 100;
        Random r1 = new Random();
        int random = 0;
        while (random < maxNum) {
            random = r1.nextInt(1000);
        }
        String unique = t + System.currentTimeMillis() + (random);
        return unique;
    }

    /**
     * 生成N位 随机数getRandom2
     *
     * @param num
     * @return String
     * @Title: getRandomx
     */
    public static String getRandomx(int num) {
        StringBuffer buffer = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            int random = rand.nextInt(KEY.length());
            char c = KEY.charAt(random);
            buffer.append(c);
        }
        return buffer.toString();
    }

    /**
     * 生成N位 整数 随机数小写字母和数字
     *
     * @param num
     * @return String
     * @Title: getRandomLow
     */
    public static String getRandomLow(int num) {
        StringBuffer buffer = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            int random = rand.nextInt(KEY.length());
            char c = KEY.charAt(random);
            buffer.append(c);
        }
        return buffer.toString();
    }

    /**
     * 通过指定的多少内的随机数及生成个数及是否从0开始,返回不重复的随机数数组
     * 
     * @param randomVal --- 多少内的随机数
     * @param arrSize   --- 数组长度
     * @param startZero --- 是否从0开始
     * @return
     * @throws IllegalAccessException
     */
    public static Integer[] random(int randomVal, int arrSize, boolean startZero) {
        if (randomVal < arrSize) {
            return null;
        } else if (randomVal <= 0) {
            return null;
        }
        Integer[] arr = new Integer[arrSize];
        for (int i = 0; true;) {
            int val = new Random().nextInt(randomVal);
            if (!startZero) {
                val += 1;
            }
            boolean hasExist = false;
            if (i > 0) {
                for (int j = i - 1; j >= 0; j--) {
                    if (val == arr[j]) {
                        hasExist = true;
                        break;
                    }
                }
            }
            if (!hasExist) {
                arr[i++] = val;
            }
            if (i == arrSize) {
                break;
            }
        }
        return arr;
    }

    /**
     * 生成N位随机数小写字母
     *
     * @param num
     * @return String
     * @Title: getRandomLowercase
     */
    public static String getRandomLowercase(int num) {
        StringBuffer buffer = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            int random = rand.nextInt(LOWERCASE.length());
            char c = LOWERCASE.charAt(random);
            buffer.append(c);
        }
        return buffer.toString();
    }

    /**
     * 生成N位随机数大写字母
     *
     * @param num
     * @return String
     * @Title: getRandomCapital
     */
    public static String getRandomCapital(int num) {
        StringBuffer buffer = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            int random = rand.nextInt(CAPITAL.length());
            char c = CAPITAL.charAt(random);
            buffer.append(c);
        }
        return buffer.toString();
    }

    public static String getRandom(int num) {
        String sRand = "";
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            String rand1 = String.valueOf(random.nextInt(10));
            sRand += rand1;
        }
        return sRand;
    }

    /**
     * 生成字符串ID
     *
     * @return String
     * @Title: getIdStr
     */
    public static String getIdStr() {
        Date date = new Date();
        long millisecond = date.getTime();
        String idStr = millisecond + "_" + RandomUtil.getRandom(10);
        return idStr;
    }

    /**
     * 获取UUID
     *
     * @return String
     * @Title: getUuid
     */
    public static String getUuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

}